﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Bus_proj.website
{
    public partial class change_ticket : System.Web.UI.Page
    {       
        string input_s;
        string date, time, date2;
        DataClasses1DataContext db = new DataClasses1DataContext();

        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years < 2300)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "/" + mount2 + "/" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            int identication = Int32.Parse(Session["Passenger_id"].ToString());
            string iden = @"select Passenger_identification
                            from Table_Passenger
                            where Table_Passenger.Passenger_id = " + identication + "";                            
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(iden, sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count <= 0)
            {
                Panel1.Visible = false;
                Response.Write(@"<script language='javascript'>alert('เกิดข้อผิดพลาดในระบบ');</script>");
            }
            else
            {
                Session["Passenger_identification"] = dt.Rows[0]["Passenger_identification"].ToString();
                Response.Redirect("change_ticket_sell.aspx");
            }           
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Table_changeticket x = new Table_changeticket();
            Select SID = new Select();
            int z = SID.GetId_change_ticket();
            x.change_id = z;
            x.change_date = DateTime.Parse(DateTime.Now.ToString("HH:mm:ss"));
            string ticket_idd = Session["ticket_id"].ToString();
            x.Sale_id = ticket_idd;
            x.change_status = true;
            db.Table_changetickets.InsertOnSubmit(x);
            db.SubmitChanges();
            
            var q = from p in db.Table_Sales
                    where p.Sale_id == ticket_idd
                    select p;
            q.ToList().ForEach(c => c.SSID = 5);
            q.ToList().ForEach(c => c.percentage = 100);
            db.SubmitChanges();

            Session["ticket_id"] = ticket_idd;
            Response.Redirect("change_ticket.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Remove("Passenger_id");
            Session.Remove("ticket_id");           
            try
            {
                string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                input_s = TextBox1.Text;
                string drive = @"select Table_Sale.Sale_id,Table_Around.Around_id,Table_Around.Around_date,Table_Route.Route_source,Table_Route.Route_destination,Table_Time.Time_time,Table_Sale.SSID,Passenger_id
                            from Table_Sale
                            INNER join Table_Around
                                on Table_Sale.Around_id = Table_Around.Around_id
                            INNER join Table_Route
                                on Table_Around.Route_id = Table_Route.Route_id
                            INNER join Table_Time
                                on Table_Around.Time_id = Table_Time.Time_id
                            where Table_Sale.Sale_id = '" + input_s + "'"; 
                SqlCommand sqlcomm = new SqlCommand();
                SqlConnection sqlconn = new SqlConnection(Connection);
                SqlDataAdapter da = new SqlDataAdapter(drive, sqlconn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count <= 0)
                {
                    Label2.Visible = true;
                    Panel1.Visible = true;
                    Button2.Visible = false;
                    Button3.Visible = false;
                }
                else
                {
                    if (dt.Rows[0]["SSID"].ToString() == "1" || dt.Rows[0]["SSID"].ToString() == "5")
                    {
                        if (dt.Rows[0]["SSID"].ToString() == "1")
                        {
                            date = ConvestToDate_check(dt.Rows[0]["Around_date"].ToString());
                            time = dt.Rows[0]["Time_time"].ToString();
                            date2 = date + " " + time + ":00";
                            string testt = ((DateTime.Parse(date2)).AddHours(-4)).ToString();
                            DateTime test = DateTime.Parse(testt);
                            if (test >= DateTime.Now)
                            {
                                string chk_change = @"select *
                                        from Table_changeticket
                                        where Table_changeticket.Sale_id =" + input_s + "";
                                SqlDataAdapter da_chk_change = new SqlDataAdapter(chk_change, sqlconn);
                                DataTable dt_chk_change = new DataTable();
                                da_chk_change.Fill(dt_chk_change);
                                if (dt_chk_change.Rows.Count <= 0)
                                {
                                    Session["Passenger_id"] = dt.Rows[0]["Passenger_id"].ToString();
                                    Session["ticket_id"] = input_s;
                                    Panel1.Visible = true;
                                    Button2.Visible = true;
                                    Button3.Visible = true;
                                    GridView1.DataSource = dt;
                                    GridView1.DataBind();
                                }
                                else
                                {

                                }
                            }
                            else
                            {
                                Panel1.Visible = false;
                                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเลื่อนตั๋วก่อนเดินทาง 4 ชั่วโมงได้');", true);
                                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเลื่อนตั๋วได้');</script>");
                            } 
                        }
                        else if (dt.Rows[0]["SSID"].ToString() == "5")
                        {
                            date = ConvestToDate_check(dt.Rows[0]["Around_date"].ToString());
                            time = dt.Rows[0]["Time_time"].ToString();
                            date2 = date + " " + time + ":00";
                            string testt = ((DateTime.Parse(date2)).AddYears(1)).ToString();
                            DateTime test = DateTime.Parse(testt);
                            if (test >= DateTime.Now)
                            {
                                string chk_change = @"select *
                                        from Table_changeticket
                                        where Table_changeticket.Sale_id =" + input_s + "";
                                SqlDataAdapter da_chk_change = new SqlDataAdapter(chk_change, sqlconn);
                                DataTable dt_chk_change = new DataTable();
                                da_chk_change.Fill(dt_chk_change);
                                if (dt_chk_change.Rows.Count <= 0)
                                {
                                    Session["Passenger_id"] = dt.Rows[0]["Passenger_id"].ToString();
                                    Session["ticket_id"] = input_s;
                                    Panel1.Visible = true;
                                    Button2.Visible = true;
                                    Button3.Visible = true;
                                    GridView1.DataSource = dt;
                                    GridView1.DataBind();
                                }
                                else
                                {
                                    //string sss = dt_chk_change.Rows[0]["change_status"].ToString();
                                    if (dt_chk_change.Rows[0]["change_status"].ToString() == "True")
                                    {
                                        Session["Passenger_id"] = dt.Rows[0]["Passenger_id"].ToString();
                                        Session["ticket_id"] = input_s;
                                        Panel1.Visible = true;
                                        Button2.Visible = true;
                                        Button3.Visible = false;
                                        GridView1.DataSource = dt;
                                        GridView1.DataBind();
                                    }
                                    else
                                    {
                                        Panel1.Visible = false;
                                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเลื่อนตั๋วได้');", true);
                                        //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเลื่อนตั๋วได้');</script>");
                                    }
                                }
                            }
                            else
                            {
                                Panel1.Visible = false;
                                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเลื่อนตั๋วได้เนื่องจากเลยกำหนด 1 ปี');", true);
                                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเลื่อนตั๋วได้');</script>");
                            } 
                        }                        
                    }
                    else
                    {
                        Panel1.Visible = false;
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเลื่อนตั๋วได้');", true);
                        //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเลื่อนตั๋วได้');</script>");
                    }                                      
                }
            }
            catch (Exception)
            {
                Panel1.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('กรุณาใส่เลขตั๋ว');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเลื่อนตั๋วได้');</script>");
            }
        }
    }
}